#include "threadPool.h"

void *threadFunc(void *arg){
    threadPool_t *pthreadPool = (threadPool_t *)arg;
    int netfd;
    while(1){
WORKER_BEGIN:
        pthread_mutex_lock(&pthreadPool->mutex);
        while(pthreadPool->taskQueue.queueSize == 0){
            pthread_cond_wait(&pthreadPool->cond,&pthreadPool->mutex);
        }
        netfd = pthreadPool->taskQueue.front->data;
        printf("one worker get netfd = %d\n",netfd);
        deQueue(&pthreadPool->taskQueue);
        pthread_mutex_unlock(&pthreadPool->mutex);// 得到netfd开始处理任务
        char msg1[1024] = "请输入用户名:";
        char msg2[1024] = "请输入密码:";
        char userName[30] = {0};
        char userPwd[30] = {0};
        send(netfd,msg1,strlen(msg1),MSG_NOSIGNAL);
        recv(netfd,userName,sizeof(userName),0);
        send(netfd,msg2,strlen(msg2),MSG_NOSIGNAL);
        recv(netfd,userPwd,sizeof(userPwd),0);
        int ret = logon(userName,userPwd);

        if(ret == 0){
            char path[1024] = "/home/shuai/test/n12test06/net-disk/server/FILE";
            pathStack_t curPathStack;
            curPathInit(&curPathStack,path);
            strcat(path,userName);
            pushStack(&curPathStack,userName);
            curPathStack.rootPathPointer++;
            char buf[4096] = {0};
            char result[4096] = {0};
            char lastPath[4096] = {0};
            char fileRealName[4096] = {0};
            int cmd,gret;
            while(1){
                bzero(buf,sizeof(buf));
                bzero(result,sizeof(result));
                bzero(&cmd,sizeof(cmd));
                recvCmd(netfd,buf,&cmd);

                switch(cmd){
                case 0:
                    close(netfd);
                    goto WORKER_BEGIN;
                case CD:
                    my_cd(buf,&curPathStack);
                    break;
                case LS:
                    my_ls(buf,&curPathStack,result);
                    sendCmd(netfd,result,LS); 
                    break;
                case PWD:
                    my_pwd(&curPathStack,result);
                    sendCmd(netfd,result,PWD);
                    break;
                case PUTS:
                    my_puts(buf,lastPath,&curPathStack);
                    printf("buf = %s,lastPath = %s\n",buf,lastPath);
                    recvFile(netfd,lastPath,&cmd);
                    break;
                case GETS:
                    gret = my_gets(buf,lastPath,&curPathStack,fileRealName);
                    if(gret == -1){
                        printf("未找到该路径!\n");
                        break;
                    }
                    sendCmd(netfd,"1",GETS);
                    sendFile(netfd,lastPath,fileRealName,GETS);
                    break;
                case MKDIR:
                    my_mkdir(buf,&curPathStack);
                    break;
                case RMDIR:
                    my_rmdir(buf,&curPathStack);
                    break;
                case REMOVE:
                    my_rmFile(buf,&curPathStack);
                    break;
                }
            }
        }else{
            close(netfd);
        }
    }
    return NULL;
}

int makeWorker(threadPool_t *pthreadPool){
    for(int i = 0; i < pthreadPool->workerArr.workerNum; i++){
        pthread_create(&pthreadPool->workerArr.tidArr[i],NULL,threadFunc,pthreadPool);
        printf("worker %d\n",i);
    }
    return 0;
}
